﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class Admin_product : System.Web.UI.Page
{
    productBus productBU = new productBus();
    UnitBU unitBU = new UnitBU();
    ManufacturerBus manuBU = new ManufacturerBus();

    string product_name;
    string product_des;
    int product_quantity;
    int unit_id;
    string image;
    int manufacturer_id;
    int price;
    bool status;
    int viewed;
    DateTime date_added;
    int sort_order;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            this.loadProduct();

        }
    }

  
    public void loadProduct()
    {
        DataTable dt = new DataTable();
        dt = productBU.getAllProductForAdminBU();
        dlProduct.DataSource = dt;
        dlProduct.DataBind();
    }
    protected void dlProduct_EditCommand(object source, DataListCommandEventArgs e)
    {
        int id = (int)e.Item.ItemIndex;
        dlProduct.EditItemIndex = id;
        loadProduct();

    }
    public void loadProductByID(int id)
    {
        DataTable dt = new DataTable();
        dt = productBU.getProductByID(id);
        dlProduct.DataSource = dt;
        dlProduct.DataBind();
    }

    public void loadUnitID(DropDownList drl)
    {
        DataTable dt = new DataTable();
        dt = unitBU.getUnitBU();
        drl.DataSource = dt;
        drl.DataTextField = dt.Columns["unit_name"].ColumnName.ToString();
        drl.DataValueField = dt.Columns["unit_id"].ColumnName.ToString();
        drl.DataBind();
    }

    public void loadManuFac(DropDownList drl)
    {
        DataTable dt = new DataTable();
        dt = manuBU.getAllManufacturer();
        drl.DataSource = dt;
        drl.DataValueField = dt.Columns["manufacturer_id"].ColumnName.ToString();
        drl.DataTextField = dt.Columns["name"].ColumnName.ToString();
        drl.DataBind();
    }

    protected void dlProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.EditItem)
        {
            DropDownList drlUnit = e.Item.FindControl("drlUnitID") as DropDownList;
            DropDownList drlManu = e.Item.FindControl("drlManuFac") as DropDownList;
            loadUnitID(drlUnit);
            loadManuFac(drlManu);
        }
    }

    protected void dlProduct_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt32(dlProduct.DataKeys[e.Item.ItemIndex]);
        product_name = ((TextBox)e.Item.FindControl("txtNameProduct")).Text.ToString();
        product_des = ((TextBox)e.Item.FindControl("txtDescription")).Text.ToString();
        product_quantity = int.Parse(((TextBox)e.Item.FindControl("txtQuantity")).Text.ToString());
        unit_id = Convert.ToInt32(((DropDownList)e.Item.FindControl("drlUnitID")).SelectedItem.Value.ToString());
        image = "~/images/" + ((Label)dlProduct.Items[dlProduct.EditItemIndex].FindControl("lblFileInfo")).Text.ToString();
        manufacturer_id = int.Parse(((DropDownList)e.Item.FindControl("drlManuFac")).SelectedItem.Value.ToString());
        price = int.Parse(((TextBox)e.Item.FindControl("txtPrice")).Text.ToString());
        status = ((CheckBox)e.Item.FindControl("cbcStatus")).Checked;
        viewed = int.Parse(((TextBox)e.Item.FindControl("txtView")).Text.ToString());
        date_added = Convert.ToDateTime(Request.Form["date_added"].ToString());
        sort_order = int.Parse(((TextBox)e.Item.FindControl("txtOrder")).Text.ToString());
        //Response.Write("" + image);
        int result = productBU.updateProductBU(id, product_name, product_des, product_quantity, 1, image, 1, price, status, date_added, viewed, sort_order);
        dlProduct.EditItemIndex = -1;
        this.loadProduct();
    }

    protected bool checkFile(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".jpg":
                return true;
            case ".bmp":
                return true;
            default:
                return false;
        }
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        dlProduct.EditItemIndex = -1;
        this.loadProduct();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        ((TextBox)dlProduct.Items[dlProduct.EditItemIndex].FindControl("txtView")).Text = "0";
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        ((TextBox)dlProduct.Items[dlProduct.EditItemIndex].FindControl("txtOrder")).Text = "0";
    }
    protected void dlProduct_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int product_id = Convert.ToInt32(dlProduct.DataKeys[e.Item.ItemIndex]);
        int result = productBU.DeleteProductBU(product_id);
        this.loadProduct();
    }



    protected void Button6_Click(object sender, EventArgs e)
    {
        string file = "";
        FileUpload fileUp = (FileUpload)dlProduct.Items[dlProduct.EditItemIndex].FindControl("fileUpload1");

        Image imgImage = (Image)dlProduct.Items[dlProduct.EditItemIndex].FindControl("Image");

        if (fileUp.HasFile)
        {
            if (checkFile(fileUp.FileName))
            {
                try
                {
                    file = fileUp.FileName.ToString().Trim();
                    //Response.Write(file);
                    if (File.Exists(Request.MapPath("~") + "\\images\\" + Server.UrlEncode(file)))
                    {
                        file = fileUp.FileName.ToString().Trim();
                        imgImage.Visible = true;
                        imgImage.ImageUrl = "~/images/" + Server.UrlEncode(file);
           
                        //Response.Write(file);
                    }
                    fileUp.SaveAs(Request.PhysicalApplicationPath + "images\\" + Server.UrlEncode(file));
                    imgImage.Visible = true;
                    imgImage.ImageUrl = "~/images/" + Server.UrlEncode(file);
                    ((Label)dlProduct.Items[dlProduct.EditItemIndex].FindControl("lblFileInfo")).Text = fileUp.FileName;

                }
                catch (System.Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
    }
}